export function initFormData(formJson) {
  const formData = {};

  function process(components) {
    components.forEach((component) => {
      if (component.hasOwnProperty('field')) {
        if (component.defaultValue !== undefined) {
          formData[component.field] = component.defaultValue;
        } else if (component.type === 'el-date-picker') {
          formData[component.field] = [];
        } else if (component.type === 'el-table') {
          formData[component.field] = [];
          return;
        } else {
          formData[component.field] = '';
        }
      }

      if (component.hasOwnProperty('fields')) {
        if (component.type === 'inline-text') {
          component.fields.forEach(field => {
            if (!(field.field in formData)) {
              formData[field.field] = '';
            }
          });
        }
      }

      if (component.hasOwnProperty('components')) {
        process(component.components);
      }
    });
  }

  process(formJson.components);
  return formData;
}

export function calculateFunction(formJson, formData) {
  // 创建一个Map对象来存储需要参与计算的项及其值
  const calculateItemMap = new Map();
  const statisticsItemMap = new Map();

  // 收集需要操作的项
  function createItemMap(components, data) {
    components.forEach((item) => {
      if (item.hasOwnProperty('field')) {
        let field = item['field'];
        if (item['isCalculateItem']) {
          calculateItemMap.set(field, data[field]);
        }
        if (item['isStatisticsItem']) {
          statisticsItemMap.set(field, data[field]);
        }
      }
      if (item.hasOwnProperty('components')) {
        createItemMap(item.components, data);
      }
    });
  }

  // 根据计算规则计算结果
  function createResult(components, data) {
    components.forEach((item) => {
      let field = item['field'];
      if (item.hasOwnProperty('components')) {
        createResult(item.components, data);
      } else {
        if (item['isCalculateResult']) {
          data[field] = calculateExpression(item['calculateExpression'], calculateItemMap);
        }
        if (item['isCalculateTableResult']) {
          let list = calculateItemMap.get(item['calculateExpression']);
          if (list && list.length > 0) {
            data[field] = list
              .map((itemT) => itemT[item['calculateColumn']])
              .reduce((acc, curr) => acc + Number(curr), 0)
              .toFixed(2);
          }
        }
      }
    });
  }

  createItemMap(formJson, formData);
  createResult(formJson, formData);
}

function calculateExpression(expression, calculateItemMap) {
  // 使用正则表达式匹配表达式中的变量名
  const variableRegex = /\b\w+\b/g;
  const variables = expression.match(variableRegex);
  // 遍历变量，替换为对应的值
  variables.forEach((variable) => {
    if (calculateItemMap.has(variable)) {
      let value = calculateItemMap.get(variable);
      // 如果值为空字符串，则将其替换为零
      if (value === '') {
        value = 0;
      }
      expression = expression.replace(variable, value);
    }
  });
  // 使用eval函数计算表达式的值，并保留两位小数
  const result = eval(expression);
  return parseFloat(result.toFixed(2));
}